from fastapi import APIRouter, HTTPException#提供提交实名认证信息的API接口
from fastapi.responses import PlainTextResponse, JSONResponse
from pydantic import BaseModel
from typing import Optional

from models.m import RealNameAuth

try:
    from qiniu import Auth as QiniuAuth
except Exception:
    QiniuAuth = None

qiniu_router = APIRouter(prefix="/qiniu", tags=["七牛上传"])
realname_router = APIRouter(prefix="/realname", tags=["实名认证"])

# 请替换为你自己的密钥与空间名
_QINIU_ACCESS_KEY = "10XJP3-oMzY2Ghrwsm6nc-fr1OKu_-HcRwweQ1AB"
_QINIU_SECRET_KEY = "L3yUOFzGq3zJwqy924JPvMg61H88COZZ8feUcAJj"
# TODO: 替换为你自己的七牛云存储空间名称
_BUCKET_NAME = "your_actual_bucket_name"

# @qiniu_router.get("/token", response_class=PlainTextResponse)#接口获取七牛云上传凭证
# async def get_qiniu_token():
#     if QiniuAuth is None:
#         raise HTTPException(status_code=500, detail="缺少qiniu依赖，请安装 qiniu")
#     q = QiniuAuth(_QINIU_ACCESS_KEY, _QINIU_SECRET_KEY)
#     token = q.upload_token(_BUCKET_NAME, expires=3600)
#     return token

class RealnameIn(BaseModel):
    user_id: int
    image_url: str
    name: str
    id_number: str
    address: str

@realname_router.post("/submit")#将用户提交的实名信息保存到数据库
async def submit_realname(payload: RealnameIn):
    record = await RealNameAuth.create(
        user_id=payload.user_id,
        image_url=payload.image_url,
        name=payload.name,
        id_number=payload.id_number,
        address=payload.address,
    )
    return {"message": "保存成功", "id": record.id}
